home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 14238 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.9 KB  |  120 lines

  1. Path: god.bel.alcatel.be!usenet
  2. From: bailleuf@btmaa.bel.alcatel.be (Frank Bailleul)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: template classes in dll? (VC++ 4.0)
  5. Date: Fri, 29 Mar 1996 16:46:37 GMT
  6. Organization: Alcatel Bell
  7. Message-ID: <4jh3u5$ek5@btmpjg.god.bel.alcatel.be>
  8. References: <1996Mar6.133456.3709@cs.mun.ca> <313E48F6.41C67EA6@sparky.hampshire.ma.us> <4iim7m$ime@news2.ios.com> <3152CB80.4106@datalytics.com>
  9. NNTP-Posting-Host: 138.203.28.170
  10. X-Newsreader: Forte Free Agent 1.0.82
  11.  
  12. Rob Stewart <stew@datalytics.com> wrote:
  13.  
  14. >Vlastimil Adamovsky wrote:
  15. >> 
  16. >> Scott Reed <scott@sparky.hampshire.ma.us> wrote:
  17. >> 
  18. >> >I'm having difficulty building a VC++ 4.0 dll with template classes.
  19. >> >The way I'm using the DllExport spec (actually __declspec(dllexport))
  20. >> >doesn't appear to fit the syntax for template classes, i.e.
  21. >> >  template <class T> class DllExport ListOf {...};
  22. >> >results in a syntax error ("error C2960: template 'class' requires a
  23. >> >tag-name"). I've searched the online help for hours but I can't figure
  24. >> >this out. I'd really appreciate any help on this.
  25. >> >        - scott
  26. >> 
  27. >> If you really insist on using templates, then try to instantaite them
  28. >> in the your DLL and then you should not get any problems using it.
  29. >> 
  30.  
  31. >To instantiate a template requires the complete source code.  
  32. >Thus, if a user of your DLL needs a specialization that is not 
  33. >otherwise present in your DLL, they will require the source 
  34. >code; the source is not exported.
  35.  
  36. >If you want to make specific instantiations part of your DLL, 
  37. >then you'll need to instantiate the template for specific 
  38. >template parameters and export them.  I'm not certain how to do 
  39. >that in VC, but there is probably help or a KB article 
  40. >explaining how to do it.  Even doing this, someone may wish to 
  41. >instantiate your template for some other template parameters.  
  42. >Therefore, you'll need to provide the complete source code 
  43. >anyway.
  44.  
  45. >If you don't intend to make specific instantiations part of your 
  46. >DLL, then there is nothing to export.  You just have to 
  47. >distribute the complete template source code with your DLL.
  48.  
  49. >Either way, the source code is visible--the one drawback to 
  50. >templates.
  51.  
  52. >-- 
  53. >Robert Stewart        | My opinions are usually my own.
  54. >Datalytics, Inc.    | stew@datalytics.com
  55.  
  56. Hello,
  57.  
  58. I've been experimenting with template classes in DLLs in Visual C++
  59. 4.0.
  60.  
  61. What I wanted to do is an export of an instantiated STL container
  62. template class : std::vector<MyClass> where MyClass is an arbitrary
  63. class.  
  64. I did that by explicitely instantiating the container template class
  65. in the header file for the DLL :
  66.  
  67. template std::vector<MyClass>;
  68.  
  69. and by using an extern template class in the header file of the client
  70. which uses the DLL :
  71.  
  72. extern template std::vector<MyClass>; 
  73. // the extern keyword is a VC++ language extension
  74.  
  75. Now in the DLL I have the function :
  76.  
  77. std::vector<MyClass> GetList();
  78.  
  79. which passes a container to the client.
  80.  
  81. In the client I write :
  82.  
  83. std::vector<MyClass> v = GetList();
  84.  
  85. This performs the following actions :
  86.  
  87. [1] call a constructor for v;
  88. [2] call a constructor in GetList() for the return value;
  89. [3] assign the return value of GetList() to v;
  90. [4] call the destructor for the return value;
  91.  
  92. The client crashes at [4].
  93.  
  94. Why ?
  95.  
  96. template<class T> class vector
  97. has a static member : allocator<T> static_allocator.
  98. Thus, in the DLL module there exists a static allocator object (call
  99. it static1) and in the client exists another static allocator object
  100. (call it static2).
  101.  
  102. [2] is constructed with allocator static1
  103. [4] is deleted with allocator static2.
  104.  
  105. The solution is obvious : delete in [4] with allocator static1.
  106. The problem is : how ?
  107. 1. Rewrite the STL code and don't use statics
  108. 2. Don't work with return values but instead write :
  109.       void GetList( std::vector<MyClass>& );
  110.  
  111. 2 will work, but then I can't use functions.
  112. 1 is something I don't want to do.
  113.  
  114. So, my question is basicly : how can I export a static ?
  115.  
  116. Thanks in advance for feedback,
  117.  
  118. Frank.
  119.  
  120.